﻿namespace Jhong.FunMVCBlog.Admin.Controllers
{
    using Jhong.FunMVC;
    using Jhong.FunMVCBlog.Core.Data;
    using Jhong.FunMVCBlog.Core.Infrastructure;
    using System;
    using System.Text;

    public class LogController : SecurityController
    {
        private LogData _logData = new LogData();

        public ActionResult List()
        {
            return View();
        }

        public ActionResult ViewLog(int id)
        {
            SetReaded(id.ToString());
            var model = this._logData.GetByID(id);
            ViewData["model"] = model;
            return View();
        }

        public ActionResult DeleteLog(string ids)
        {
            return null;
        }

        public ActionResult SetReaded(string ids)
        {
            var sb = new StringBuilder();
            foreach (var id in ids.Split(','))
            {
                if (string.IsNullOrWhiteSpace(id)) continue;
                sb.Append(id);
                sb.Append(",");
            }
            bool res = true;
            if (sb.Length == 0) goto n1;
            sb.Remove(sb.Length - 1, 1);
            this._logData.SetRead(sb.ToString());
        n1: return AjaxResult(res, res ? "设置成功" : "设置失败");
        }

        public ActionResult GetData(int pageIndex, int itemsPerPage, DateTime? startTime, DateTime? endTime, string logType, string logFrom)
        {
            var data = this._logData.GetList(pageIndex, itemsPerPage, startTime, endTime, logType, logFrom);
            var page = data.GetPageHelper();
            return AjaxResult(true, data: new { Items = data.Items, Page = page.GetPageHtml() });
        }
    }
}